/**
 * @file lv_obj_matrix_demo.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lvgl.h"
#include "lv_obj_matrix.h"

/*********************
 *      DEFINES
 *********************/


/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/


/**********************
 *  STATIC VARIABLES
 **********************/


/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

static void button_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {


    }
}

static void create_cell_cb(lv_obj_t * matrix, uint16_t hpos, uint16_t vpos)
{
    lv_obj_t * cell_container = lv_obj_matrix_get_cell_container(matrix, hpos, vpos);

    lv_obj_t * button = lv_button_create(cell_container);
    //lv_obj_set_size(button, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    //lv_obj_set_size(button, 64, 48);
    lv_obj_set_size(button, LV_PCT(90), LV_PCT(90));
    lv_obj_set_align(button, LV_ALIGN_CENTER);
    lv_obj_t  *label = lv_label_create(button);
    char buf[4];
    buf[0] = vpos + '1'; buf[1] = '-'; buf[2] = hpos + '1';  buf[3] = '\0';
    lv_label_set_text(label, buf);
    lv_obj_set_align(label, LV_ALIGN_CENTER);
    lv_obj_add_event_cb(button,  button_event_handler, LV_EVENT_CLICKED, matrix);
}

static void event_cell_cb(lv_obj_t *matrix, lv_event_code_t event_code, int16_t hpos, int16_t vpos)
{
    if((hpos < 0) || (vpos < 0)) return;
    lv_obj_t * cell_container = lv_obj_matrix_get_cell_container(matrix, hpos, vpos);
    if(cell_container == NULL) return;
    lv_obj_t * label = lv_obj_get_child(lv_obj_get_child(cell_container, 0), 0);
    char buf[4];
    buf[0] = vpos + '1'; buf[1] = '-'; buf[2] = hpos + '1';  buf[3] = '\0';
    lv_label_set_text(label, buf);
    lv_obj_invalidate(label);
}

/**<Matrix object distance from outer frame margin */
static const struct _lv_obj_matrix_info_t info_auto_space = {
    create_cell_cb, NULL, event_cell_cb,
    {8, 8, 8, 8},
    {64, 48},     /**< the size of the cell container, 0:auto max size */
    8,          /**< Number of horizontal objects */
    8,          /**< Number of vertical objects */
    0,          /**< when hcount or vcount is 0, max count*/
    LV_ALIGN_CENTER,       /**< align */
};

/**<Matrix object distance from outer frame margin */
static const struct _lv_obj_matrix_info_t info_auto_size = {
    create_cell_cb, NULL, event_cell_cb,
    {8, 8, 8, 8},
    {0, 0},     /**< the size of the cell container, 0:auto max size */
    4,          /**< Number of horizontal objects */
    4,          /**< Number of vertical objects */
    0,          /**< when hcount or vcount is 0, max count*/
    LV_ALIGN_TOP_MID,       /**< align */
};

/**<Matrix object distance from outer frame margin */
static const struct _lv_obj_matrix_info_t info_auto_hv_right = {
    create_cell_cb, NULL, event_cell_cb,
    {8, 8, 8, 8},
    {64, 48},     /**< the size of the cell container, 0:auto max size */
    0,          /**< Number of horizontal objects */
    0,          /**< Number of vertical objects */
    -64,          /**< when hcount or vcount is 0, max count*/
    LV_ALIGN_TOP_MID,       /**< align */
};

/**<Matrix object distance from outer frame margin */
static const struct _lv_obj_matrix_info_t info_auto_hv_buttom = {
    create_cell_cb, NULL, event_cell_cb,
    {8, 8, 8, 8},
    {64, 48},     /**< the size of the cell container, 0:auto max size */
    0,          /**< Number of horizontal objects */
    0,          /**< Number of vertical objects */
    64,          /**< when hcount or vcount is 0, max count*/
    LV_ALIGN_TOP_MID,       /**< align */
};

/**<Matrix object distance from outer frame margin */
static const struct _lv_obj_matrix_info_t info_auto_ver = {
    create_cell_cb, NULL, event_cell_cb,
    {8, 8, 8, 8},
    {0, 48},     /**< the size of the cell container, 0:auto max size */
    4,          /**< Number of horizontal objects */
    0,          /**< Number of vertical objects */
    64,          /**< when hcount or vcount is 0, max count*/
    LV_ALIGN_TOP_MID,       /**< align */
};

/**<Matrix object distance from outer frame margin */
static const struct _lv_obj_matrix_info_t info_auto_hori = {
    create_cell_cb, NULL, event_cell_cb,
    {8, 8, 8, 8},
    {64, 0},     /**< the size of the cell container, 0:auto max size */
    0,          /**< Number of horizontal objects */
    4,          /**< Number of vertical objects */
    -64,          /**< when hcount or vcount is 0, max count*/
    LV_ALIGN_TOP_MID,       /**< align */
};

void lv_obj_matrix_demo(uint32_t mode)
{
    const struct _lv_obj_matrix_info_t *info;
    if(mode == 0) info = &info_auto_space;
    else if(mode == 1) info = &info_auto_size;
    else if(mode == 2) info = &info_auto_hv_right;
    else if(mode == 3) info = &info_auto_hv_buttom;
    else if(mode == 4) info = &info_auto_ver;
    else if(mode == 5) info = &info_auto_hori;
    else return;
    lv_obj_t * obj_matrix = lv_obj_matrix_create(lv_screen_active());
    lv_obj_matrix_set_info(obj_matrix, info);
    lv_obj_matrix_update(obj_matrix);
}
